package com.example.administrator.zhongyichou.help;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.lzy.ninegrid.ImageInfo;
import com.lzy.ninegrid.preview.ImagePreviewActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lichenyu on 2017/11/24.
 * 描述：预览大图
 */

public class Preview {
    /**
     * @param activity        上下文
     * @param list            图片源
     * @param position        预览位置
     * @param adapterView
     * @param statusBarHeight 状态栏高度
     */
    public static void preview(Activity activity, List<String> list, int position, AdapterView adapterView, int statusBarHeight) {
        List<ImageInfo> imageInfo = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            ImageInfo info = new ImageInfo();
            info.bigImageUrl = list.get(i);
            info.thumbnailUrl = list.get(i);
            //拿到view的位置
            View imageView;
            imageView = adapterView.getChildAt(position);
            info.imageViewWidth = imageView.getWidth();
            info.imageViewHeight = imageView.getHeight();
            int[] points = new int[2];
            imageView.getLocationInWindow(points);
            info.imageViewX = points[0];
            info.imageViewY = points[1] - statusBarHeight;
            imageInfo.add(info);
        }
        // 启动预览.
        Intent intent = new Intent(activity, ImagePreviewActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(ImagePreviewActivity.IMAGE_INFO, (Serializable) imageInfo);
        bundle.putInt(ImagePreviewActivity.CURRENT_ITEM, position);
        intent.putExtras(bundle);
        activity.startActivity(intent);
        activity.overridePendingTransition(0, 0);
    }
}
